package com.zhao.service.serviceImpl;

import com.zhao.dao.StudentDao;
import com.zhao.dao.TradeRecordsDao;
import com.zhao.dao.UniversityDao;
import com.zhao.pojo.*;
import com.zhao.service.TradeRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TradeRecordsServiceImpl implements TradeRecordsService {

    @Autowired
    private TradeRecordsDao tradeRecordsDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private UniversityDao universityDao;

    private TradeRecordsImpl tradeRecordsToTradeRecordsImpl(TradeRecords tradeRecords){
        TradeRecordsImpl tradeRecords1 = new TradeRecordsImpl();
        tradeRecords1.setTradeRecords(tradeRecords);
        String payerID = tradeRecords.getPayerID();
        University university = universityDao.findByID(tradeRecords.getUniversityID());
        Student student = studentDao.findByStudentID(payerID,tradeRecords.getUniversityID());
        if(student==null){

            tradeRecords1.setPayerName(university.getUniversityName());
        }else{
            tradeRecords1.setPayerName(student.getName());
        }
        String receiverID = tradeRecords.getReceiverID();
        student = studentDao.findByStudentID(receiverID,tradeRecords.getUniversityID());
        if(student==null){
            tradeRecords1.setReceiveName(university.getUniversityName());
        }else{
            tradeRecords1.setReceiveName(student.getName());
        }

        return tradeRecords1;
    }

    @Override
    public String adminGetAll(Admin admin, HttpSession session) {

        List<TradeRecords> tradeRecordsList = tradeRecordsDao.findByUniversityID(admin.getUniversityID());
        List<TradeRecordsImpl> tradeRecordsImplList = new ArrayList<>();
        for(TradeRecords tradeRecords : tradeRecordsList){
            TradeRecordsImpl tradeRecords1 = tradeRecordsToTradeRecordsImpl(tradeRecords);
            tradeRecordsImplList.add(tradeRecords1);
        }
        session.setAttribute("tradeRecordsImplList",tradeRecordsImplList);
        return "success";
    }

    @Override
    public String schoolCounsellorGetAll(SchoolCounsellor schoolCounsellor, HttpSession session) {
        List<TradeRecords> tradeRecordsList = tradeRecordsDao.findByUniversityID(schoolCounsellor.getUniversityID());
        List<TradeRecordsImpl> tradeRecordsImplList = new ArrayList<>();
        //System.out.println(tradeRecordsList);
        for(TradeRecords tradeRecords : tradeRecordsList){
            Student student = studentDao.findByStudentID(tradeRecords.getPayerID(),schoolCounsellor.getUniversityID());
            if(student!=null && student.getMajorID().equals(schoolCounsellor.getMajorID())){
                TradeRecordsImpl tradeRecords1 = tradeRecordsToTradeRecordsImpl(tradeRecords);
                tradeRecordsImplList.add(tradeRecords1);
            }
            student = studentDao.findByStudentID(tradeRecords.getReceiverID(),schoolCounsellor.getUniversityID());
            if(student!=null && student.getMajorID().equals(schoolCounsellor.getMajorID())){
                TradeRecordsImpl tradeRecords1 = tradeRecordsToTradeRecordsImpl(tradeRecords);
                tradeRecordsImplList.add(tradeRecords1);
            }

        }
        //System.out.println(tradeRecordsImplList);
        session.setAttribute("tradeRecordsImplList",tradeRecordsImplList);
        return "success";
    }

    @Override
    public String adminDeleteOneTR(Admin admin, String orderFormID, String payerID, String receiverID, HttpSession session) {
        int res = tradeRecordsDao.deleteOne(orderFormID,payerID,receiverID,admin.getUniversityID());
        if(res!=1){
            return "删除失败";
        }
        return "success";
    }


}
